package id.co.rho.scoring.cid.server.command.handler.impl;

import org.springframework.context.ApplicationContext;

import id.co.rho.scoring.cid.client.command.impl.ModelSearchCommandAction;
import id.co.rho.scoring.cid.client.command.impl.ModelSearchCommandAction.SearchType;
import id.co.rho.scoring.cid.client.command.impl.ModelSearchCommandResult;
import id.co.rho.scoring.cid.server.command.handler.CommandHandler;
import id.co.rho.scoring.cid.server.service.ModelSearchService;
import id.co.rho.scoring.cid.server.util.AppContext;

public class ModelSearchCommandHandler implements
		CommandHandler<ModelSearchCommandAction, ModelSearchCommandResult> {

	@Override
	public ModelSearchCommandResult execute(ModelSearchCommandAction command) {
		ApplicationContext ctx = AppContext.getApplicationContext();
		ModelSearchService service = ctx.getBean(ModelSearchService.class);
		ModelSearchCommandResult result = new ModelSearchCommandResult();
		if (command.getSearchType().name()
				.equalsIgnoreCase(SearchType.MAINSEARCH.name())) {
			result = service.searchCriteria(command.getName(),
					command.getType(), command.getRevision(), Boolean.TRUE);
		} else if (command.getSearchType().name()
				.equalsIgnoreCase(SearchType.VERSIONING.name())) {
			result = service.searchCriteria(command.getName(),
					command.getType(), command.getRevision());
		}
		return result;
	}

	@Override
	public Class<ModelSearchCommandAction> getActionType() {
		return ModelSearchCommandAction.class;
	}

}
